package com.linkage.educloud.ah.adapter.dynamic;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.DynamicCommentBean;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.widget.CommonDialog;
import com.linkage.educloud.ah.widget.ExpressionUtils;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/9/18.
 */

public class DynamicCommentAdapter extends android.widget.BaseAdapter {
    private List<DynamicCommentBean> mcomments;
    private Context mcontext;
    private OnClickListener onClickListener;

    int mfrom = 1;

    public DynamicCommentAdapter(Context context, List<DynamicCommentBean> comments, int from) {
        this.mfrom = from;
        this.mcontext = context;
        this.mcomments = comments;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (mfrom == 1) {
            count = mcomments.size() > 5 ? 5 : mcomments.size();
        } else if (mfrom == 2) {
            count = mcomments.size();
        }
        return count;
    }

    @Override
    public DynamicCommentBean getItem(int position) {
        return mcomments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.common_one_textview, null);
            holder = new ViewHolder();
            holder.text1 = convertView.findViewById(R.id.textview_one_common1);
            holder.text2 = convertView.findViewById(R.id.textview_one_common2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final DynamicCommentBean bean = getItem(position);
        holder.text1.setText(Html.fromHtml("<font color='#ff0D92DD'>" + bean.getUser_name() + "</font>" + ":\t"));
        holder.text2.setText(ExpressionUtils.replaceFace(mcontext,
                bean.getContent()));
        //列表界面不让点击
        if (mfrom == 2) {
            holder.text2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onItemClick(position);
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        TextView text1;
        TextView text2;
    }

    //点赞回调接口
    public interface OnClickListener {
        void onItemClick(int position);
    }

    public void setOnClickListener(OnClickListener onItemActionClick) {
        this.onClickListener = onItemActionClick;
    }
}

